/**
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function (nums) {
  if (!nums || !nums.length) return 0
  let currentSum = nums[0]
  let maxSun =nums[0]
  for (let i = 0; i < nums.length; i++) {
    //如果 currentSum + nums[i] 更大，则继续累加。否则，丢弃之前的子数组，从 nums[i] 重新开始。
    currentSum = Math.max(nums[i],(nums[i]+currentSum))
    maxSun = Math.max(maxSun,currentSum)
  }
  return maxSun
}
const nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
const result = maxSubArray(nums)
console.log('🚀 ~ result:', result)
